package com.iwhalecloud.bss.kite.cucc.service.attr.handler;

import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalKeyConsts;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteProdInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.iwhalecloud.bss.kite.service.attr.handler.AbstractAttrHandler;
import com.iwhalecloud.bss.kite.service.scene.attr.OperateInstAttrService;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * Description:天津附加商品资费中价格处理类
 * File Name:PriceAttrHandler
 * Modification History:
 * Date             Author      Version     Description
 * ------------------------------------------------------------------
 * 2022-3-15 9:17   邱萌         1.0       1.0 Version
 **/
public class PriceAttrHandler extends AbstractAttrHandler<KiteInst, AttrData> {

    @Autowired
    private IQuerySceneInstService querySceneInstService;
    @Autowired
    OperateInstAttrService operateInstAttrService;

    @Override
    public void def(KiteAttr attr, KiteInst inst, AttrData data) {

        if(KiteStringUtils.isEmpty(attr.getValue())) {
            //获取对应资费的订购个数
            List<KiteProdInst> kiteProdInstList1 = querySceneInstService.listProdInstsFromOfferInst(data.getCustId(), inst.getInstId());
            String orderNum = kiteProdInstList1.get(0).getAttrValue(LocalKeyConsts.ATTR_200003299);
            //获取资费单价
            String price = inst.getAttrValue(LocalKeyConsts.UNIT_PRICE);
            //计算价格：因为价格允许修改、如果价格已存在则不按照 订购个数*单价（元）
            if (KiteStringUtils.isNotEmpty(price) && KiteStringUtils.isNotEmpty(orderNum)) {
                BigDecimal djValueBd = new BigDecimal(price);
                BigDecimal gsValueBd = new BigDecimal(orderNum);
                BigDecimal attrValueBd = djValueBd.multiply(gsValueBd);
                String attrValueString = attrValueBd.setScale(0, BigDecimal.ROUND_HALF_DOWN).toString();
                attr.setDefaultValue(attrValueString);
            }
        }

        super.def(attr, inst, data);
    }





}
